/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package batallanaval;

import gnu.io.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Morse
 */
public class DriverRedSerial implements DriverRed {

    InputStream inPort;        // Canal de entrada (De -> Puerto)
    OutputStream outPort;       // Canal de salida  (Puerto -> Hacia)
    String interfazID = "COM1";
    SerialPort mySerialPort;
    CommPortIdentifier cpi;

    public void enviar(String trama) {
        try {
            outPort.write(trama.getBytes());

        } catch (IOException ex) {
            Logger.getLogger(DriverRedSerial.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void inicializarConexion(String conexion) {

        try {
            interfazID = "COM1";
            cpi = CommPortIdentifier.getPortIdentifier(interfazID);
            mySerialPort = (SerialPort) cpi.open("Serial Port", 2000);


            mySerialPort.setSerialPortParams(
                    9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            inPort = mySerialPort.getInputStream();
            outPort = mySerialPort.getOutputStream();


        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }

    public String recibir() {
        try {

            String msg1 = "";
            int x = 0;
            x = inPort.available();
            while (x != 124) {
                x = inPort.read();
                if (x != -1) {
                    String msg = new Character((char) x).toString();
 //                   System.out.print(msg);
                    msg1 = msg1 + msg;
                }
            }
                   System.out.print(msg1);
            return msg1;
        } catch (IOException ex) {
            Logger.getLogger(DriverRedSerial.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }

    }

    public String recibir2() {
        try {
            String msg1 = "";
            byte[] b = new byte[10];
            int x = 0;
            int y = 0;

            do {
                x = inPort.read(b);
                y = inPort.available();

                if (x != -1) {
                    if (x != 0) {
                        msg1 = new String(b);
                    }
                }
            } while (y == 0);


            return msg1;
        } catch (IOException ex) {
            Logger.getLogger(DriverRedSerial.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }

    }

    public void terminarConexion() {
        try {
            outPort.close();
            inPort.close();
            mySerialPort.close();
        } catch (IOException ex) {
            Logger.getLogger(DriverRedSerial.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
